<?php
/**
 * Mission Tasks
 * 
 * @package Mission\Task
 * @author Xavier VILAIN
 */
use Hive\Company;
use Fox\Behaviors;
use Fox\Generator;
use Fox\Html\Button;
use Fox\Html\Header\Content;
/* @var $contact Hive\Ressource */
$company = Xview::instance()->component('company', 'object');
$reference = $company->getId();
// Customizing
$customizing = Hive\Company\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'company','act'=>'tasks'));

// Page Title
$header = new Content('tasks',Msg(Hive::BUNDLE,Hive::TITLE_TASKS));
$header->add(new Button('add','?'))
		->behaviors(Behaviors::action(
				Generator::url('company','tasks','edit',array('reference' => $reference)),
				true,null,null,true));
				
// Load All open tasks
$tasks = $company->tasks();
$html = null;
if(count($tasks) > 0 )
{
	$futureTasks = $weekTasks = $todayTasks = $lateTasks = null;
	$today = new DateTime();
	$now = $today->format('Ymd');
	$thisWeek = $today->format('Yw');

	foreach($tasks as $task)
	{
		$planDate = new DateTime($task->estimate_duedate);
		$date = $planDate->format('Ymd');
		$week = $planDate->format('Yw');
		$title = $task->subject;
		$taskid = $task->taskid;
		$text = $task->comment;
		
		$dateString = Fox\Utilities\Dates::dateTimeToFullString($task->estimate_duedate);
		$subTitle = Msg(Hive::BUNDLE,Hive::MSG_INTERIEW_PLAN_SUBTITLE,User::fullName($task->assigned_to),$dateString);
		
		// Update
		$behaviorUpdate = new Behaviors(Behaviors::action(Generator::url('missions','tasks','edit',
									array('reference' => $reference,'taskid' => $taskid)),true,null,null,true));
		$behaviorUpdate->add(Behaviors::value('slink'));
		// Delete
		$behaviorDelete = new Behaviors(Behaviors::action(
								Generator::url('tasks',
											   'tickmark',
												null,
												array('reference' => $reference,
													  'taskid' => $taskid)),
								false,null,null,false,'POST',null,
								'#tid_'.$taskid,null,true
								));
		$behaviorDelete->add(Behaviors::value('slink del'));
								
	/*	
	  $behaviorDelete = new Behaviors(Behaviors::action(Generator::url('ressource','tasks','delete',
									array('rid' => $rid,'taskid' => $taskid)),true,null,null,true));
		$behaviorDelete->add(Behaviors::value('slink del'));
	*/
		$li = '<li id="tid_'.$taskid.'" class="mbs pbm noteEntry uiListItem uiListLight uiListVerticalItemBorder">
				<div class="mvs fsl fwb fcb">
					<span class="uiListItemTitle">'.$title.'</span>
				</div>
				<div class="pbm fsm fwn fcg">
					'.$subTitle.' &#8226; 
					<span class="UIActionLinks UIActionLinks_bottom">
						<a href="#" id="upd_'.$taskid.'" class="'.$behaviorUpdate->serialize(array(),'').'">'.Msg(Hive::BUNDLE,Hive::UPDATE).'</a>
						 &#8226; <a href="#" id="del_'.$taskid.'" class="'.$behaviorDelete->serialize(array(),'').'">'.Msg(Hive::BUNDLE,Hive::MARK_AS_DONE).'</a>
					</span>
				</div>';
		if(!empty($text)) {
			$li .= '<div class="clearfix interview-text">
					<div>
						<p>'.Fox\Utilities\Text::toHtml(stripslashes($task->comment)).'</p>
					</div>
				</div>';
		}
		$li .= '</li>';
		if($date < $now)
		{
			$lateTasks .= $li;
		}
		elseif($date == $now)
		{
			// Today 
			$todayTasks .= $li;
			
		}
		elseif($week == $thisWeek)
		{
			// Later this Week
			$weekTasks .= $li;
		}
		else 
		{
			// Later this month
			$futureTasks .= $li;
		}
	}
	
	if(!empty($lateTasks))
	{
		$html .= '<div class="uiHeader uiHeaderTopAndBottomBorder uiHeaderSection">
					<div class="clearfix uiHeaderTop">
						<div><h3 class="uiHeaderTitle">'.Msg(Hive::BUNDLE,Hive::TITLE_LATE_TASKS).'</h3></div>
					</div>
				</div>';
		$html .= '<ul>'.$lateTasks.'</ul>';
	}
	if(!empty($todayTasks))
	{
		$html .= '<div class="uiHeader uiHeaderTopAndBottomBorder uiHeaderSection">
					<div class="clearfix uiHeaderTop">
						<div><h3 class="uiHeaderTitle">'.Msg(Hive::BUNDLE,Hive::TITLE_TODAY).'</h3></div>
					</div>
				</div>';
		$html .= '<ul>'.$todayTasks.'</ul>';
	}
	if(!empty($weekTasks))
	{
		$html .= '<div class="uiHeader uiHeaderTopAndBottomBorder uiHeaderSection">
					<div class="clearfix uiHeaderTop">
						<div><h3 class="uiHeaderTitle">'.Msg(Hive::BUNDLE,Hive::TITLE_THIS_WEEK).'</h3></div>
					</div>
				</div>';
		$html .=  '<ul>'.$weekTasks.'</ul>';
	}
	if(!empty($futureTasks))
	{
		$html .= '<div class="uiHeader uiHeaderTopAndBottomBorder uiHeaderSection">
					<div class="clearfix uiHeaderTop">
						<div><h3 class="uiHeaderTitle">'.Msg(Hive::BUNDLE,Hive::TITLE_LATER).'</h3></div>
					</div>
				</div>';
		$html .= '<ul>'.$futureTasks.'</ul>';
	}
}
else
{
	$html = '<div class="text-center">'.Msg(Hive::BUNDLE,Hive::MSG_NO_TASKS_PLAN).'</div>';
}
?>
<div id="tasksContent">
	<div><?php echo $header->render();?></div>
	<div class="contentArea">
		<div class="mbs gsl">
				<?php echo $html;?>
		</div>
	</div>
</div>